﻿using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
using Gdmec.Util;
using Gdmec.Config;

namespace Gdmec.Hot
{
    public class HotfixManager : Hotfix
    {
        public static HotfixManager Instance { get; private set; }
        void Awake()
        {
            Instance = this;
        }
        void OnDestroy()
        {
            Instance = null;
        }


        public Version localVersion;
        public Version oldVersion;
        public Version newVersion;

        public System.Action onFinish;
        public static void Hot(System.Action onCall) 
        {
            string url = GameConfig.ToStreamingAssetPath("version.txt");
            Instance.StartCoroutine(WebUtil.GetText(url, (bool isOk, string text) =>
            {
                if (isOk)
                {
                    Instance.localVersion = JsonUtil.FromJson<Version>(text);
                    Hot0();
                }
            }));
            Instance.onFinish = onCall;
        }

        public static void Hot0()
        {
            string url = GameConfig.ToHotFixPath("version.txt");
            Instance.StartCoroutine(WebUtil.GetText(url, (bool isOk, string text) =>
            {
                if (isOk)
                {
                    Instance.oldVersion = JsonUtil.FromJson<Version>(text);
                }
                Hot1();
            }));
        }

        public static void Hot1() 
        {
            string url = "http://192.168.3.28:80/version.txt";
            Instance.StartCoroutine(WebUtil.GetText(url, (bool isOk, string text) =>
            {
                if (isOk)
                {
                    Instance.newVersion = JsonUtil.FromJson<Version>(text);
                    Hot2();
                }
            }));
        }

        public static void Hot2()
        {
            var nowVersion = Instance.localVersion;
            if (Instance.oldVersion != null)
            {
                nowVersion = Instance.oldVersion;
            }
            if (Instance.newVersion != null)
            {
                if (Instance.newVersion.version > nowVersion.version)
                {
                    Instance.StartCoroutine(WebUtil.GetBytes(Instance.newVersion.url, (bool isOk, byte[] bytes) =>
                    {
                        if (isOk)
                        {
                            ZipUtil.UpZipByBytes(bytes, GameConfig.HotFixFilePath);
                        }
                        JsonUtil.CreateJsonFile(GameConfig.HotFixFilePath + "/version.txt", Instance.newVersion);
                        if (Instance.onFinish != null) Instance.onFinish();
                    }));
                }
                else 
                {
                    if (Instance.onFinish != null) Instance.onFinish();
                }
            }
        }
    }

    public class Version 
    {
        public long version;
        public string url;
        public override string ToString()
        {
            return string.Format("version:{0}\br url:{1}",version,url);
        }
    }
}
